package com.bigfreakindeals.Controller;

import java.util.ArrayList;

import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import com.bigfreakindeals.Activities.ActiveDealsActivityGroup;
import com.bigfreakindeals.Activities.GetSchoolAllActivity;
import com.bigfreakindeals.Activities.MainTabActivity;
import com.bigfreakindeals.Activities.NoDealsActivity;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Common.Tools;
import com.bigfreakindeals.Models.ActiveDeal;
import com.bigfreakindeals.Models.ObjectImage;
import com.bigfreakindeals.Models.UniversityDeals;
import com.bigfreakindeals.Protocols.SoapHandlers.BeansSaysHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.ClosestSchoolHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.FAQHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.GetSchoolListHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.GetTodaysDealHandler;

public class UIController implements ImageListener{	
	public  Context current;
	public UIController(){
	
		
	}
	public void getFAQ()
	{
		new Thread() {
			public void run() {
				try {
					sleep(5);
				} catch (Exception e) {
				}
				
				new FAQHandler(Tools.getURL(Constant.GET_FAQ,null));	
			}

		}.start();
		
	}	
	
	public void callNodealsPage(String error){
		
		
		ApplicationEx.getInstance().nodeasIntent =  new Intent(ApplicationEx.getInstance().controller.current, NoDealsActivity.class);  
		Log.i("NODEALS ACTIVITY " ,"calls no deals -set NO DEALS UNIVERSTY");
		
		ApplicationEx.getInstance().myProfile.getlastSelectedSchool().hasDeals =false;
			
		if (ApplicationEx.getInstance().myProfile.getHomeUniversity().getSchoolSlug() == 
			ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolSlug() )
			
			ApplicationEx.getInstance().myProfile.getHomeUniversity().hasDeals = false;
		
		UniversityDeals universitydeals =  ApplicationEx.getInstance().myProfile.getlastSelectedSchool();
		Log.i("NODEALS ACTIVITY " ,"NO DEALS UNIVERSTY is set"+universitydeals.getSchoolName());
				
		 
		 Intent tabIntent = new Intent(ApplicationEx.getInstance().controller.current,MainTabActivity.class);
		 	ApplicationEx.getInstance().controller.current.startActivity(tabIntent);
		 
	}
	
	public void getClosestSchool(Context context, boolean isAGPS){
		ApplicationEx.getInstance().controller.current =  context;
		System.out.println("UICONTROLLER GET CLOSEST SCHOOLS SPLASH");
   		
		final ArrayList<String> params = new ArrayList<String>();
		if (isAGPS) {
			params.add(ApplicationEx.getInstance().myProfile.university_preference_Lat);
			params.add(ApplicationEx.getInstance().myProfile.university_preference_Long);
			
		}else {
			
			params.add("47.663635");
			params.add("-122.313477");
			
		}
		
		
		new Thread() {
			public void run() {
				try {
					sleep(5);
				} catch (Exception e) {
				}
				
				
				new ClosestSchoolHandler(Tools.getURL(Constant.GET_CLOSESTSCHOOL,params), new ClosestSchoolListener() {
					
					@Override
					public void handlerResponse(boolean isSuccess,boolean isRefresh) {
						// TODO Auto-generated method stub
						System.out.println("UICONTROLLER RESPOSNE");
				   		getTodaysDeal(isRefresh);
					}
				},true);
				
			}

		}.start();

	}
	
	
	public void getMainTabActivity(){
		
		//get all thumbnails;
		
		Intent tabIntent = new Intent(ApplicationEx.getInstance().controller.current,MainTabActivity.class);
		ApplicationEx.getInstance().controller.current.startActivity(tabIntent);
		
	//	fetchImages(ApplicationEx.getInstance().myProfile.getlastSelectedSchool());
		
		
	}
	public void getSchoolListings(boolean isdisplay){
		
		new Thread() {
			public void run() {
				try {
					sleep(5);
				} catch (Exception e) {
				}
				new GetSchoolListHandler(Tools.getURL(Constant.GET_SCHOOLLIST, null));
			}

		}.start();
	}
	public void getBeansSays(){
		 
		new Thread() {
			public void run() {
				try {
					sleep(500);
				} catch (Exception e) {
				}
				new BeansSaysHandler(Tools.getURL(Constant.GET_BEANSCOMMENT,null));
			}

		}.start();
		
	}
	public void getTodaysDeal(String slug, final boolean isReFresh){
		
		
	 	
        
		final ArrayList<String> params = new ArrayList<String>();
		params.add(slug);
		new Thread() {
			public void run() {
				try {
					sleep(5);
				} catch (Exception e) {
				}
				new GetTodaysDealHandler(Tools.getURL(Constant.GET_TODAYS_DEAL, params),isReFresh);
			}

		}.start();
		
	}
	
	public void getTodaysDeal(boolean isRefresh){
		String univId = ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolSlug();
		getTodaysDeal(univId,isRefresh);
	
	}
	

public BroadcastReceiver broadcastreceiever = new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(Constant.EVENTS_GETACTIVEDEALS_ERROR)) {
			// update UI
			Log.i("RECEIVED","ApplicationEx.getInstance().controller.getTodaysDeal();@UICONTROOLER");
		}
	}
};

public void fetchImages(UniversityDeals universityDeals) {
	Log.i("fetchImage"," university deals");
	ArrayList<ActiveDeal> items = universityDeals.getDeals();

	for (int i = 0; i < items.size(); i++) {
		ActiveDeal item = items.get(i);
		getImage(item.getMediumImgUrl(), item.getDealID(),universityDeals.getSchoolSlug());
	}

}

public void getImage(final String feed, final String dealId,
		final String universitySlug) {
	final ImageListener listener = this;
	// In all cases, start a background thread.
	Thread thread = new Thread() {
		public void run() {
			try {
				Thread.sleep(1);
				ImageEngine.getInstance().getImage(new ObjectImage(feed, listener, dealId,universitySlug));
			} catch (InterruptedException ex) {
			}
		}

	};
	thread.start();
}

@Override
public void imageResponse(Bitmap bitmap, String imgId, String universityId) {

	// TODO Auto-generated method stub
	Log.i("BROADCAST RESPONSE", "LSTENER ACTIVATED" + "imgID = " + imgId+ " " + universityId + "" + bitmap);

	if (bitmap != null && imgId != null && universityId != null) {
		if (universityId == ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolSlug()) {
			if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool().dealHashMap.containsKey(imgId)) {
				ActiveDeal updatedDeal = ApplicationEx.getInstance().myProfile.getlastSelectedSchool().dealHashMap.get(imgId);
				updatedDeal.thumbPromoImage = bitmap;
				ApplicationEx.getInstance().myProfile.getlastSelectedSchool().updateHashMap(updatedDeal,imgId);
			}
			
			Intent intent = new Intent(Constant.EVENTS_GETBITMAPIMAGE);
			ApplicationEx.getInstance().sendBroadcast(intent);
		}

	}
}
}
